package org.qydjk.console.controller.basic;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.ServeLabel;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 服务标签管理c
 * Created by Edward_yang on 2017/12/25 0025.
 */
@Controller
@RequestMapping("/serverLabel")
public class ServerLabelController extends BaseController{
    /***
     * 跳转服务标签页面
     * @author Edward_yang
     * @uptime 2017-12-25
     * @return
     */
    @RequestMapping("/gotoServerLabel.do")
    public String  gotoServerLabel(){
    return "/basic/sellerAndproduct/serverLabelList";
    }

    /***
     * 服务列表数据
     * @author Edward_yang
     * @uptime 2017-12-25
     * @return
     */
    @RequestMapping("/serverLabelList.do")
    @ResponseBody
    public Object serverLabelList(Integer rows,Integer page,String keyWord){
        HibernatePage pager = new HibernatePage(page,rows);
        String sql="SELECT  qsl.id as id ,qsl.class_property_font as classPropertyFont ,qsl.class_property_name as  classPropertyName ,qsl.class_property_img as classPropertyImg ,qsl.class_property_text as classPropertyText , qsl.detail as detail ,qsl.ord as ord,qsl.create_time as createTime " +
                " FROM  qy_serve_label qsl  WHERE 1=1";
        String countSql="SELECT count(qsl.id) FROM  qy_serve_label qsl WHERE 1=1";
        //查询
        if(!StringUtil.isEmptyOrNull(keyWord)){
            sql+="  and qsl.classPropertyName like '%"+keyWord+"%'";
            countSql+=" and spl.classPropertyName like '%"+keyWord+"%'";
        }
        sql+= " ORDER BY  qsl.create_time desc";
        try {
            List<Map<String,Object>> sellerList=baseService.findMapBySQL(sql,pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total,sellerList);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /***
     * 添加
     * @author Edward_yang
     * @uptime 2017-12-25
     * @return
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Object addServerLabel(ServeLabel serveLabel){
        try {
            serveLabel.setCreateTime(new Date());//创建时间
            serveLabel.setUpdateTime(new Date());
            baseService.save(serveLabel);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /***
     * 编辑
     * @author Edward_yang
     * @uptime 2017-12-25
     * @return
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object editServerLabel(ServeLabel serveLabel){
        try {
            ServeLabel serveLabel1=baseService.get(ServeLabel.class,serveLabel.getId());
            BeanUtil.copyProperties(serveLabel,serveLabel1);
            serveLabel1.setUpdateTime(new Date());
            baseService.saveOrUpdate(serveLabel1);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
        return renderSuccess();
    }
    /***
     * 删除
     * @author Edward_yang
     * @uptime 2017-12-25
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object deleteServerLabel(Integer id){
        try {
            ServeLabel serveLabel=baseService.get(ServeLabel.class,id);
            baseService.delete(serveLabel);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /***
     * 编辑描述
     * @param detail
     * @param id
     * @return
     */
    @RequestMapping("/editDescribe.do")
    @ResponseBody
    public Object editDescribe(String detail ,Integer id){
        try {
            ServeLabel serveLabel1=baseService.get(ServeLabel.class,id);
            serveLabel1.setDetail(detail);
            serveLabel1.setUpdateTime(new Date());
            baseService.saveOrUpdate(serveLabel1);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
        return renderSuccess();
    }
}
